home *** CD-ROM | disk | FTP | other *** search
- program TestSort;
-
- {$IFDEF Windows}
- !! Error - 32-bit only
- {$ENDIF}
-
- {$APPTYPE CONSOLE}
-
- uses
- Windows,
- SysUtils,
- SortFns;
-
- const
- MaxEIndex = 9999;
-
- type
- PSortArray = ^TSortArray;
- TSortArray = array [0..MaxEIndex] of TSortElement;
-
- procedure RandomizeSA(var SA : PSortArray);
- var
- i : integer;
- begin
- for i := 0 to MaxEIndex do
- SA^[i] := Trunc(Random * 1.0e6);
- end;
-
- function CheckOrder(var SA : PSortArray) : boolean;
- var
- i : integer;
- begin
- Result := false;
- for i := 1 to MaxEIndex do
- if SA^[i] < SA^[i-1] then
- Exit;
- Result := true;
- end;
-
- function LessThan(const X, Y : TSortElement) : boolean;
- begin
- Result := X < Y;
- end;
-
- const
- SortName : array [0..7] of string[21] =
- ('Bubble sort ',
- 'Shaker sort ',
- 'Selection sort ',
- 'Usual insertion sort ',
- 'Insertion sort ',
- 'Shellsort ',
- 'Usual quicksort ',
- 'quicksort ');
-
- var
- SA : PSortArray;
- StartTime : integer;
- i : integer;
- ElapsedTime : array [0..7] of integer;
-
- begin
- FillChar(ElapsedTime, sizeof(ElapsedTime), 0);
- try
- New(SA);
- try
- {bubble}
- if (MaxEIndex < 10000) then begin
- RandomizeSA(SA);
- StartTime := GetTickCount;
- BubbleSort(SA^, 0, MaxEIndex, LessThan);
- ElapsedTime[0] := GetTickCount - StartTime;
- if not CheckOrder(SA) then
- writeln('*** Bubble sort failed');
- end;
- {shaker}
- if (MaxEIndex < 10000) then begin
- RandomizeSA(SA);
- StartTime := GetTickCount;
- ShakerSort(SA^, 0, MaxEIndex, LessThan);
- ElapsedTime[1] := GetTickCount - StartTime;
- if not CheckOrder(SA) then
- writeln('*** Shaker sort failed');
- end;
- {selection}
- if (MaxEIndex < 10000) then begin
- RandomizeSA(SA);
- StartTime := GetTickCount;
- SelectionSort(SA^, 0, MaxEIndex, LessThan);
- ElapsedTime[2] := GetTickCount - StartTime;
- if not CheckOrder(SA) then
- writeln('*** Selection sort failed');
- end;
- {usual insertion}
- if (MaxEIndex < 10000) then begin
- RandomizeSA(SA);
- StartTime := GetTickCount;
- UsualInsertionSort(SA^, 0, MaxEIndex, LessThan);
- ElapsedTime[3] := GetTickCount - StartTime;
- if not CheckOrder(SA) then
- writeln('*** Usual insertion sort failed');
- end;
- {insertion}
- if (MaxEIndex < 10000) then begin
- RandomizeSA(SA);
- StartTime := GetTickCount;
- InsertionSort(SA^, 0, MaxEIndex, LessThan);
- ElapsedTime[4] := GetTickCount - StartTime;
- if not CheckOrder(SA) then
- writeln('*** Insertion sort failed');
- end;
- {shellsort}
- RandomizeSA(SA);
- StartTime := GetTickCount;
- Shellsort(SA^, 0, MaxEIndex, LessThan);
- ElapsedTime[5] := GetTickCount - StartTime;
- if not CheckOrder(SA) then
- writeln('*** Shellsort failed');
- {usual quicksort}
- RandomizeSA(SA);
- StartTime := GetTickCount;
- UsualQuickSort(SA^, 0, MaxEIndex, LessThan);
- ElapsedTime[6] := GetTickCount - StartTime;
- if not CheckOrder(SA) then
- writeln('*** Usual quicksort failed');
- {quicksort}
- RandomizeSA(SA);
- StartTime := GetTickCount;
- QuickSort(SA^, 0, MaxEIndex, LessThan);
- ElapsedTime[7] := GetTickCount - StartTime;
- if not CheckOrder(SA) then
- writeln('*** quicksort failed');
-
- for i := 0 to 7 do begin
- writeln(SortName[i], ElapsedTime[i]:10);
- end;
- finally
- Dispose(SA);
- end;
- except
- on E: Exception do
- writeln(E.Message);
- end;
- readln;
- end.
-